package leecode;

/**
 * 第13天，双指针
 */
public class Leetcode13 {
    //第一题
    public int[] exchange(int[] nums) {
        int l=0;
        int r=nums.length-1;
        while (l<r){
            while(l<nums.length-1&&nums[l]%2!=0){
                l++;
            }
            while (r>0&&nums[r]%2==0){
                r--;
            }
            if(l<r){
                int tmp=nums[l];
                nums[l]=nums[r];
                nums[r]=tmp;
                l++;
                r--;
            }
        }
        return nums;
    }
    //第二题
    public int[] twoSum(int[] nums, int target) {
        int l=0;
        int r=nums.length-1;
        int []arr=new int[2];
        while (l<r){
            int sum=nums[l]+nums[r];
            if(sum>target){
                r--;
            }else if(sum<target){
                l++;
            }
            if(sum==target) {
                arr[0]=nums[l];
                arr[1]=nums[r];
                return arr;
            }
        }
        return new int[0];
    }
//第三题
    public String reverseWords(String s) {
    String[]str=s.trim().split(" ");
    StringBuilder str2=new StringBuilder();
        for (int i = str.length-1; i>=0; i--) {
            if(str[i].equals("")){
                continue;
            }
            str2.append(str[i]+" ");
        }
        return str2.toString();
    }
    public static void main(String[] args) {
        Leetcode13 leetcode13=new Leetcode13();
        int[]arr={2,7,11,15};
        leetcode13.twoSum(arr,9);
    }
}
